package com.hr.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.hr.dao.PositionDao;
import com.hr.pojo.Position;
import com.hr.pojo.Salary;
import com.hr.utils.C3P0Utils;

public class PositionDaoImpl implements PositionDao {
	private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
	@Override
	public List findAllPosition() {
		String sql ="select * from hr_position";
		List<Position> list = null;
		try {
			 list = queryRunner.query(sql, new BeanListHandler<Position>(Position.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}


	@Override
	public void updatePosition(Position position) {
		String sql ="update hr_position set name = ?,salary= ?,department= ? where id = ?";
		Object[] params = new Object[]{position.getName(),position.getSalary(),position.getDepartment(),position.getId()};
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void deletePosition(String id) {
		String sql ="delete from hr_position where id = ? ";
		try {
			queryRunner.update(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	@Override
	public void saveSalary(Position position) {
		String sql ="insert into hr_position(name,salary,department) values(?,?,?)";
		Object[] params = new Object[]{position.getName(),position.getSalary(),position.getDepartment()};
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	@Override
	public Position findPositionById(String id) {
		String sql ="select * from hr_position where id = ? ";
		Position position = null;
		try {
			position = queryRunner.query(sql, new BeanHandler<>(Position.class), id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return position;
	}
}